//
//  LoginApi.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/21.
//

import Foundation
import Moya


enum MCLoginApi {
    case wechatApi(code:String?)
    case appleApi(nickName:String?,identiftyToken:String?)
    case mobileApi(mobile:String?,verif:String?)
    case mobileSend(mobile:String)
    case mobileBindSend(mobile:String?)
    case mobileBind(mobile:String?,code:String?)
}

extension MCLoginApi:TargetType {
    
    var baseURL: URL {
        return URL.init(string: Moya_baseURL)!
    }
    
    var path: String {
        switch self {
        case .appleApi:
            return "api/app/register/loginByApple"
        case .wechatApi:
            return  "api/app/register/loginByWx"
        case .mobileApi:
            return "api/app/register/login"
        case .mobileSend:
            return "api/app/register/sendCode"
        case .mobileBindSend:
            return "api/app/user/bindPhoneSendCode"
        case .mobileBind:
            return "api/app/user/bindPhone"
        }
    }
    var method:Moya.Method {
        switch self {
        case .wechatApi:
            return .post
        case .appleApi:
            return .post
        case .mobileSend:
            return .post
        case .mobileApi:
            return .post
        case .mobileBindSend:
            return .post
        case .mobileBind:
            return .post
        }
    }
    
    var sampleData: Data {
        return Data()
    }
    
    
    var task: Task {
        switch self {
        case let .wechatApi(code):
            return .requestParameters(parameters: ["code":code ?? ""], encoding: JSONEncoding.default)
        case let .appleApi(nickName, identiftyToken):
            return .requestParameters(parameters: ["identityToken":identiftyToken ?? "","nickName":nickName ?? ""], encoding: JSONEncoding.default)
        case let .mobileSend(mobile):
            return .requestParameters(parameters: ["phone":mobile], encoding: JSONEncoding.default)
        case let .mobileApi(mobile, verif):
            return .requestParameters(parameters: ["phone":mobile ?? "","code":verif ?? ""], encoding: JSONEncoding.default)
        case let .mobileBind(mobile, code):
            return .requestParameters(parameters: ["phone":mobile ?? "","code":code ?? ""], encoding: JSONEncoding.default)
        case let .mobileBindSend(mobile):
            return .requestParameters(parameters: ["phone":mobile ?? ""], encoding: JSONEncoding.default)
        }
    }
    
    var headers: [String : String]? {
        
        let tokenName:String = UserDefaults.standard.string(forKey: "LoginName") ?? ""
        let tokenValue:String = UserDefaults.standard.string(forKey: "LoginToken") ?? ""
        switch self {
        case .mobileBindSend,.mobileBind:
            return [tokenName:tokenValue]
        default:
            return [:]
        }
        
    }
    
    
}
